//存放主要交互逻辑
//javascript 模块化
var seckill ={
	//封装秒杀相关ajax的url
	URL: {
		now : function(){
			return '/b/seckill/time/now';
		},
		exposer : function(seckillId){
			return '/b/seckill/' + seckillId + '/exposer';
		},
		execution : function(seckillId, md5){
			return '/b/seckill/' + seckillId + '/' + md5 +'/execution';
		}
	},
	//秒杀逻辑
	startKill: function(seckillId, node){
		//获取秒杀地址，控制实现逻辑，执行秒杀
		node.hide()
			.html('<button class="btn btn-primary btn-lg" id="killBtn">开始秒杀</button>'); //按钮
		$.post(seckill.URL.exposer(seckillId), {}, function(result){
			if(result && result['success']){
				var exposer = result['data'];
				console.log(exposer);
				if(exposer['isExposed']){
					//开启秒杀,获取到秒杀地址
					var md5 = exposer['md5'];
					killUrl = seckill.URL.execution(seckillId, md5);
					console.log("killUrl:"+killUrl);
					//绑定一次点击事件，防止多次点击
					$('#killBtn').one('click',function(){
						//执行秒杀请求
						//1.禁用按钮
						$(this).addClass('disabled'); 
						//2.发送秒杀请求,执行秒杀
						$.post(killUrl, {}, function(result){
							if(result && result['success']){
								var killResult = result['data'];
								var state = killResult['state'];
								var stateInfo = killResult['stateInfo'];
								//3.显示秒杀结果
								node.html('<span class="label label-success">'+stateInfo+'</span>');
							}
						});
					});
					node.show();
				}else{
					//未开启秒杀
					var now = exposer['now'];
					var start = exposer['start'];
					var end = exposer['end'];
					//可能用户的时间与系统有误差，再走一遍计时事件
					seckill.countdown(seckillId, now, start, end);
				}
			}else{
				alert("秒杀出错!");
				console.log(result);
			}
		});
	},
	//电话号码验证
	validatePhone: function(phone){
		//isNan():验证是不是非数字，是非数字的话取true
		if(phone && phone.length == 11 && !isNaN(phone)){
			return true;
		}else{
			return false;
		}
	},
	//计时模块
	countdown: function(seckillId, nowTime, startTime, endTime){
		var seckillBox = $('#seckill-box');
		//时间判断
		if(nowTime > endTime){
			//秒杀结束
			seckillBox.html('秒杀结束！');
		}else if(nowTime < startTime){
			alert("开始倒计时");
			//秒杀未开始,计时事件绑定
			var killTime = new Date(startTime + 1000); //把毫秒转换成date类型，+1s是为了防止用户时间偏移
			//调用插件的countdown()事件
			seckillBox.countdown(killTime, function(event){ //当时间变化时回调函数帮我们做日期的输出
				//时间格式化
				var format = event.strftime('秒杀倒计时：%D天 %H时 %M分 %S秒');
				seckillBox.html(format);
			}).on('finish.countdown', function(){ //时间完成后的回调事件
				//获取秒杀地址，控制实现逻辑，执行秒杀
				seckill.startKill(seckillId, seckillBox);
			});
		}else{
			//秒杀开始
			seckill.startKill(seckillId, seckillBox);
		}
	},
	//秒杀商品详情逻辑
	detail : {
		//详情页初始化
		init : function(params){
			//手机验证和登陆，计时交互
			//规划交互流程
			//在cookie中查找手机号
			var killPhone = $.cookie('killPhone'); // 因为引入了cookie的js
			if(!seckill.validatePhone(killPhone)){ //验证手机号
				//绑定phone
				var killPhoneModal = $('#killPhoneModel');
				//弹出层
				killPhoneModal.modal({
					show : true,  //显示弹出层
					backdrop : 'static', //禁止点击旁边关闭弹出层
					keyboard : false, //关闭键盘事件
				});
				$('#killPhoneBtn').click(function(){
					var inputPhone = $('#killPhoneKey').val();
					if(seckill.validatePhone(inputPhone)){
						//把电话写入cookie,有效期为7天，只在seckill模块有效
						$.cookie('killPhone', inputPhone, {expires : 7, path : '/b/seckill'});
						console.log("killPhone:"+$.cookie('killPhone'));
						alert($.cookie('killPhone'));
						//刷新页面
						window.location.reload();
					}else{
						//先隐藏，填充内容后显示出来
						$('#killPhoneMessage').hide().html('<label class="label label-danger">手机号错误！</label>').show(300);
					}
				});
			}
			//已经登陆
			//计时交互
			var startTime = params['startTime']; //javascript访问json数据的一种方式
			var endTime = params['endTime']; 
			var seckillId = params['seckillId']; 
			$.get(seckill.URL.now(), {}, function(result){
				if(result && result['success']){
					console.log(result);
					var nowTime = result['data'];
					//时间判断,进入计时模块
					seckill.countdown(seckillId, nowTime, startTime, endTime);
				}else{
					console.log(resutl);
					alert("获取系统时间出错");
				}
			});
		}
	}
};